<?php
/**
 * 美财大学
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class university_detailControl extends MobileControl {
	private $voucherstate_arr;
	
	public function __construct(){
		parent::__construct();
		$this->isLogin();
	}
	//美财大学视频展示
	public function indexOp(){
		$study_id = $_GET['study_id'];
		$member_id = $_SESSION['member_id'];
		$study_cat_id = universityModel::getStudy_cat_id($study_id);
		$model_studys =new universityModel();
		$sql = "select s.*,sc.* from #__study  as s left join #__study_category as sc on s.study_cat_id = sc.study_cat_id where s.study_id = '".$study_id."'";
			$pagesize = 8;
		$curpage = $_GET['curpage'];
		$rs=db::pageQuery($sql, $curpage, $pagesize);
        $university_list = $rs->fetchAll();
        $page = new Page();
        $page->setEachNum($pagesize);
        $page->setNowPage($curpage);
		  $page->setTotalNum($rs->pageRows());
        $page->setStyle('admin');
        Tpl::output('university_list',$university_list);
		Tpl::output('study_id',$study_id);
		Tpl::output('member_id',$member_id);
        Tpl::output('page',$page->show());
		$sql1="SELECT * FROM #__study where study_cat_id = '".universityModel::getStudy_cat_id($study_id)."' ORDER BY rand() ";
	      	$pagesize = 4;
			$curpage = $_GET['curpage'];
	      	$rs=db::pageQuery($sql1, $curpage, $pagesize);
	        $university_list_fenlei = $rs->fetchAll();
	        $page = new Page();
	        $page->setEachNum($pagesize);
	        $page->setNowPage($curpage);
			$page->setTotalNum($rs->pageRows());
	        $page->setStyle('admin');
	        Tpl::output('university_list_fenlei',$university_list_fenlei);
	        Tpl::output('page1',$page->show());
					//视频推荐
			$param['order'] = 'study_id desc';
			$fieldstr = "study_id,study_cat_id,study_price,title,study_photo";
			$where = " where study_cat_id = '".$study_cat_id."'";
		//优先得到推荐视频
        $studys_commend_list = $model_studys->getStudys($param, '', $fieldstr, 'study_free', $extend, $where);
		Tpl::output('studys_commend',$studys_commend_list);
		Tpl::showPage('u_detail');
	}
	//预存款购买视频界面
	public function lesson_orderOp(){
		$study_id = $_GET['study_id'];
		$study_cat_id = $_GET['study_cat_id'];
		Tpl::output('study_id',$study_id);
		Tpl::output('study_cat_id',$study_cat_id);
		Tpl::showPage('lesson_order');
		
		
		
	}
		//积分购买视频
	public function lesson_pointerorderOp(){
		$study_id = $_GET['study_id'];
		$study_cat_id = $_GET['study_cat_id'];
		Tpl::output('study_id',$study_id);
		Tpl::output('study_cat_id',$study_cat_id);
		Tpl::showPage('lesson_pointerorder');
	}
	
	//预存款购买视频
	public function lesson_buyOrderDeatilOp(){
			$study_id = $_GET['study_id'];	
				$member_id = $_SESSION['member_id'];
				$study_cat_id = $_GET['study_cat_id'];
		$member_available_predeposit =universityModel::getMember_available_predeposit($member_id);
			$study_price =universityModel::getstudy_price($study_id);
				$update_price = 	$member_available_predeposit - $study_price;
				if($update_price<0){
					showMessage("余额不足，购买失败");
				}else{
					if(Db::queryRow("select study_id from #__study_log where study_id = '".$study_id."' GROUP by member_id")){
						showMessage("你已经购买此视频","index.php?act=university_detail&op=index&study_id=".$study_id."&study_cat_id=".$study_cat_id);
					}else{
					if(Db::update ('member',array('available_predeposit'=>$update_price), "member_id=".$member_id )){
						$param ['study_id'] = $study_id;
						$param ['buy_time'] = time();
						$param ['member_id'] = $member_id;
						$param ['buy_price'] = $study_price;
						$param ['member_name'] = trim($_SESSION['member_name']);
						if(Db::insert ('study_log',$param)){
							showMessage("购买成功！","index.php?act=university_detail&op=index&study_id=".$study_id."&study_cat_id=".$study_cat_id);
						}else{
							showMessage("购买失败，请重试！");
						}
					}else{
						showMessage("购买失败，请重试！");
					}
					}
				}
		
		
		
	}
	
	
	
	//积分兑换
	public function lesson_pointerOrderDetailOp(){
				$study_id = $_GET['study_id'];	
				$member_id = $_SESSION['member_id'];
				$study_cat_id = $_GET['study_cat_id'];
				$member_points =universityModel::getMember_points($member_id);
				$study_points =universityModel::getstudy_price($study_id);//课程的价格
				$update_points = 	$member_points - $study_points;
				if($update_points<0){
					showMessage("积分不足，购买失败");
				}else{
					if(Db::queryRow("select study_id from #__study_log where study_id = '".$study_id."' GROUP by member_id")){
						showMessage("你已经购买此视频","index.php?act=university_detail&op=index&study_id=".$study_id."&study_cat_id=".$study_cat_id);
					}else{
					if(Db::update ('member',array('member_points'=>$update_points), "member_id=".$member_id )){
						$param ['study_id'] = $study_id;
						$param ['buy_time'] = time();
						$param ['member_id'] = $member_id;
						$param ['buy_points'] = $study_points;
						$param ['member_name'] = trim($_SESSION['member_name']);
						
						
						if(Db::insert ('study_log',$param)){
							showMessage("购买成功！","index.php?act=university_detail&op=index&study_id=".$study_id."&study_cat_id=".$study_cat_id);
							
						}else{
							showMessage("购买失败，请重试！");
						}
						
					}else{
						showMessage("购买失败，请重试！");
					}
						
					}
				}
				
				
	}

//积分兑换订单详情
		public function lesson_pointsOrderDetailOp(){
			$study_id = $_GET['study_id'];	
				$member_id = $_SESSION['member_id'];
				Tpl::output('study_id',$study_id);
				Tpl::output('member_id',$member_id);
				Tpl::showPage('lesson_pointerOrderDetail');
			
			
		}
		//预存款购买订单详情
		public function lesson_orderDetialOp(){
			$study_id = $_GET['study_id'];
			$member_id = $_SESSION['member_id'];
			Tpl::output('study_id',$study_id);
			Tpl::output('member_id',$member_id);
			Tpl::showPage('lesson_orderdetail');
		}
	//视频收藏
		public function study_collectionOp(){
			$study_id = $_POST['study_id'];
			$study_cat_id = universityModel::getStudy_cat_id($study_id);
			$member_id = $_SESSION['member_id'];
			$param['collection_study_id'] = $study_id;
			$param['member_id'] = $member_id;
			$param['study_cat_id'] = $study_cat_id;
			$param['collection_time'] = time();
			if(Db::queryRow("select collection_study_id from #__study_collection where collection_study_id = '".$study_id."' GROUP by member_id")){
				$arr['success'] =2;
			}else{
					$rs = Db::insert ('study_collection', $param);
			if($rs){
				$arr['success'] =1;
			} else{
				$arr['success'] = 0;
			}
			}
		
			echo  json_encode($arr);
		}
		//取消视频收藏
		public function cancel_study_collectionOp(){
			$study_id = $_POST['study_id'];
			$member_id = $_SESSION['member_id'];
			$where = "where collection_study_id = '".$study_id."' and member_id = '".$member_id."'";
			$rs = Db::delete('study_collection', $where );
			if($rs){
				$arr['success'] = 1;
			} else{
				$arr['success'] = 0;
			}
			echo  json_encode($arr);
		}
		//判断是否已经收藏了视频
		public function collection_judgeOp(){
			$study_id = $_POST['study_id'];
			$member_id = $_SESSION['member_id'];
			$rs = Db::queryALL("select * from #__study_collection where collection_study_id = '".$study_id."' and member_id = '".$member_id."'");
			if($rs){
				$arr['success'] = 1;
			} else{
				$arr['success'] = 0;
			}
			echo  json_encode($arr);
		}
		
			//视频链接显示，如果用户购买视频成功，向用户发送视频链接
		public function video_linkOp(){
					$study_id = $_POST['study_id'];
			$member_id = $_SESSION['member_id'];
			$video = universityModel::getVideo_link($study_id);
			$rs = Db::queryALL("select * from #__study_log where study_id = '".$study_id."' and member_id = '".$member_id."'");
			if($rs){
				$arr['success'] = 1;
				$arr['video'] = $video;
//				Tpl::output('title',$video);
			} else{
				$arr['success'] = 0;
			}
			echo  json_encode($arr);
		}
			
			
			
		
		
		
		
		
		//更新视频播放次数
		public function update_study_playOp(){
			$study_id = $_POST['study_id'];
			if(Db::update ('study',array('study_play'=>universityModel::getstudy_play($study_id)+1), "study_id=".$study_id )){
								$arr['success'] = 1;
		}else{
							$arr['success'] = 0;
		}
					echo  json_encode($arr);
		

}
		

	
	
	
}
